package uia.alumni.profile;

import java.io.IOException;
import java.sql.Date;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import uia.alumni.data.CvEntry;
import uia.alumni.data.Role;
import uia.alumni.data.User;
import uia.alumni.web.Command;


/**
 *
 * @author Even Åby Larsen (even.larsen@uia.no)
 */
public class SaveCvEntry extends Command implements Constants {
    /** SERVLET_NAME is used for consistency checking of web.xml */
    public static final String SERVLET_NAME = "profile.savecventry";

    @PersistenceUnit(name=PERSISTENCE_UNIT)
    private EntityManagerFactory emf;

    @Resource
    private UserTransaction tx;


    public SaveCvEntry() {
        super(Role.member);
    }


    /**
     * Save the data from the EditCvEntry form.
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void execute(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        // get parameters
        String id = request.getParameter(CVENTRY);
        String type = request.getParameter(TITLE);
        String fromDay = request.getParameter(FROMDATE+"_d");
        String fromMonth = request.getParameter(FROMDATE+"_m");
        String fromYear = request.getParameter(FROMDATE+"_y");
        Date fromDate = getDate(fromYear, fromMonth, fromDay);
        String toDay = request.getParameter(TODATE+"_d");
        String toMonth = request.getParameter(TODATE+"_m");
        String toYear = request.getParameter(TODATE+"_y");
        Date toDate = getDate(toYear, toMonth, toDay);
        String description = request.getParameter(DESCRIPTION);

        try {
            tx.begin();
            EntityManager em = emf.createEntityManager();
            User user = getLoggedInUser(em, request);

            long cveId = parseLong(id, -1);
            CvEntry cve;
            if (cveId == -1) {
                cve = new CvEntry();
                user.addCvEntry(cve);
            }
            else cve = user.getCvEntry(cveId);
            
            if (cve != null) {
                cve.setType(type);
                cve.setDescription(description);
                cve.setFromDate(fromDate);
                cve.setToDate(toDate);
                tx.commit();
            }
            else {
                addMessage(request,
                        "CV entry was not saved - illegal ID!");
                tx.rollback();
            }
        }
        catch (Exception e) {
            addMessage(request,
                    "Saving CV entry failed, because: "+e.getMessage());
        }
        forwardTo(EditCv.SERVLET_NAME, request, response);
    }


    public final static long serialVersionUID = 1;

}
